package com.common.utils.file;
import com.common.excel.ExcelUtil;
import com.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件处理工具类
 * 
 *
 */
public class FileUtils extends org.apache.commons.io.FileUtils
{
    public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";

    /**
     * 输出指定文件的byte数组
     * 
     * @param filePath 文件路径
     * @param os 输出流
     * @return
     */
    public static void writeBytes(String filePath, OutputStream os) throws IOException
    {
        FileInputStream fis = null;
        try
        {
            File file = new File(filePath);
            if (!file.exists())
            {
                throw new FileNotFoundException(filePath);
            }
            fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int length;
            while ((length = fis.read(b)) > 0)
            {
                os.write(b, 0, length);
            }
        }
        catch (IOException e)
        {
            throw e;
        }
        finally
        {
            if (os != null)
            {
                try
                {
                    os.close();
                }
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }
            }
            if (fis != null)
            {
                try
                {
                    fis.close();
                }
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除文件
     * 
     * @param filePath 文件
     * @return
     */
    public static boolean deleteFile(String filePath)
    {
        boolean flag = false;
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists())
        {
            file.delete();
            flag = true;
        }
        return flag;
    }

    /**
     * 文件名称验证
     * 
     * @param filename 文件名称
     * @return true 正常 false 非法
     */
    public static boolean isValidFilename(String filename)
    {
        return filename.matches(FILENAME_PATTERN);
    }

    /**
     * 检查文件是否可下载
     * 
     * @param resource 需要下载的文件
     * @return true 正常 false 非法
     */
    public static boolean checkAllowDownload(String resource)
    {
        // 禁止目录上跳级别
        if (StringUtils.contains(resource, ".."))
        {
            return false;
        }

        // 检查允许下载的文件规则
        if (ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource)))
        {
            return true;
        }

        // 不在允许下载的文件规则
        return false;
    }

    /**
     * 下载文件名重新编码
     * 
     * @param request 请求对象
     * @param fileName 文件名
     * @return 编码后的文件名
     */
    public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
    {
        final String agent = request.getHeader("USER-AGENT");
        String filename = fileName;
        if (agent.contains("MSIE"))
        {
            // IE浏览器
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
        }
        else if (agent.contains("Firefox"))
        {
            // 火狐浏览器
            filename = new String(fileName.getBytes(), "ISO8859-1");
        }
        else if (agent.contains("Chrome"))
        {
            // google浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        }
        else
        {
            // 其它浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;
    }

    /**
     * 下载文件名重新编码
     *
     * @param response 响应对象
     * @param realFileName 真实文件名
     * @return
     */
    public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException
    {
        String percentEncodedFileName = percentEncode(realFileName);

        StringBuilder contentDispositionValue = new StringBuilder();
        contentDispositionValue.append("attachment; filename=")
                .append(percentEncodedFileName)
                .append(";")
                .append("filename*=")
                .append("utf-8''")
                .append(percentEncodedFileName);

        response.setHeader("Content-disposition", contentDispositionValue.toString());
    }

    /**
     * 百分号编码工具方法
     *
     * @param s 需要百分号编码的字符串
     * @return 百分号编码后的字符串
     */
    public static String percentEncode(String s) throws UnsupportedEncodingException
    {
        String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
        return encode.replaceAll("\\+", "%20");
    }

    /**
     * 根据文件路径读取byte[] 数组
     */
    public static byte[] readFileByBytes(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
            BufferedInputStream in = null;

            try {
                in = new BufferedInputStream(new FileInputStream(file));
                short bufSize = 1024;
                byte[] buffer = new byte[bufSize];
                int len1;
                while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
                    bos.write(buffer, 0, len1);
                }

                byte[] var7 = bos.toByteArray();
                return var7;
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException var14) {
                    var14.printStackTrace();
                }

                bos.close();
            }
        }
    }
    public static  byte[] getFileByte(String path,int position){
        try{
            byte[] byteData=readFileByBytes(path);
            int size = 1024;
            int totalSize=byteData.length;
            System.out.println("=======totalSize :" + totalSize);
            int startPosition=position*size;
            if(startPosition>totalSize){
                return new byte[0];
            }
            if(startPosition+size>totalSize){
                size=totalSize-startPosition;
            }
            System.out.println("=======size :" + size);
            System.out.println("=======startPosition :" + startPosition);
            byte[] newByte=new byte[size];
            System.arraycopy(byteData,startPosition,newByte,0,size);

            //FE 0E 长度 00 01
//            String data=HexadecimalConver.byte2HexStr(newByte);
            return newByte;
//            String sixteen = String.format("%04x", Integer.valueOf(size+2));
        }catch (Exception e){
            e.printStackTrace();;
        }
        return new byte[0];
    }

    public static void writeFile(String message,String filePath){
//        String file = "d:/test/gps.txt";
        try {
            // 创建FileWriter对象
            FileWriter fileWriter = new FileWriter(filePath, true);

            // 创建BufferedWriter对象
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            // 写入内容
            bufferedWriter.write(message);
            bufferedWriter.newLine();

            // 关闭BufferedWriter和FileWriter
            bufferedWriter.close();
            fileWriter.close();

            System.out.println("写入成功！");
        } catch (IOException e) {
            System.out.println("写入失败：" + e.getMessage());
        }
    }

    /**
     * 从提交的excel文件地址解析数据
     * @author tm
     * @date 2024-03-15 11:31
     * @param fileUrl
     * @return List<List<Object>>
     */
    public static List<List<Object>> getExcelListByFileUrl(String fileUrl){
        List<List<Object>> list = new ArrayList<>();
        try{
            String filePath = "";
            if (StringUtils.isNotBlank(fileUrl) && fileUrl.indexOf("upload") > -1) {
                int index = fileUrl.indexOf("upload");
                filePath = "/zhnl/ftp/" + fileUrl.substring(index, fileUrl.length());
            }

            File file1 = new File(filePath);
            String fileName1 = filePath.split("\\/")[filePath.split("\\/").length-1];
            FileInputStream in1 =new FileInputStream(file1);
            list = ExcelUtil.getBankListByExcel(in1,fileName1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
